package cn.itcast.service.system.impl;


import cn.itcast.dao.system.DeptDao;
import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DeptDao deptDao;


    //查询
    @Override
    public PageInfo findAll(int page, int size, String companyId) {
        PageHelper.startPage(page,size);
        List<User> list = userDao.findAll(companyId);
        return new PageInfo(list);
    }
    //查询部门
    @Override
    public List<Dept> findAll(String id) {
        List<Dept> list = deptDao.findAll(id);
        return list;
    }

    //添加
    @Override
    public void save(User user) {
        System.out.println(user);
        user.setId(UUID.randomUUID().toString());

        userDao.save(user);
    }

    //id查询
    @Override
    public User findById(String id) {

        User user = userDao.findById(id);

        return user;
    }

    //修改
    @Override
    public void update(User user) {
        userDao.update(user);
    }
    //删除
    @Override
    public void delete(String id) {
        userDao.delete(id);
    }

    //根据id查询角色
    @Override
    public List<String> roleFindById(String id) {
        List<String> list = userDao.roleFindById(id);
        return list;
    }

    //修改角色
    @Override
    public void changeRole(String userid, String[] roleIds) {

        userDao.deleteUserRole(userid);

        for (String roleId : roleIds) {
            userDao.saveUserRole(userid,roleId);
        }
    }

    //根据邮箱查用户
    @Override
    public User findByEmail(String email) {
        User user = userDao.findByEmail(email);
        return user;
    }


}
